import random
from math import sin, cos, pi, log
from tkinter import *

CANVAS_WIDTH = 640  # 画布宽度
CANVAS_HEIGHT = 480  # 画布高度
CANVAS_CENTER_X = CANVAS_WIDTH / 2  # 画布中心x轴坐标
CANVAS_CENTER_Y = CANVAS_HEIGHT / 2  # 画布中心y轴坐标
IMAGE_ENLARGE = 11  # 放大比例
HEART_COLOR = "#FF7171"  # 画笔颜色，这里是个淡红色

class Heart:
    # 爱心
    def __init__(self, generate_frame=20):
        self._points = set()  # 爱心上的点坐标集合
        self._edge_diffusion_points = set()  # 边缘扩散效果点集合
        self._center_diffusion_points = set()  # 中心扩散效果点集合
        self.all_points = {}  # 每帧动态点坐标
        self.build(2000)

        self.random_halo = 1000

        self.generate_frame = generate_frame
        for frame in range(generate_frame):
            self.calc(frame)

        # 文本属性
        self.text = "天天开心"  # 添加的自定义文字
        self.text_font = ("Helvetica", 36, "bold")  # 文本字体和大小
        self.text_color = "pink"  # 文本颜色

    def build(self, number):
        # 构建爱心坐标点
        for _ in range(number):
            t = random.uniform(0, 2 * pi)  # 随机获取时间参数t
            x, y = self.heart_function(t)
            self._points.add((x, y))

        # 爱心边缘扩散点
        for _ in range(number):
            x, y = random.choice(list(self._points))
            x, y = self.scatter_inside(x, y, 0.17)
            self._edge_diffusion_points.add((x, y))

        # 爱心内扩散点
        point_list = list(self._points)
        for _ in range(4000):
            x, y = random.choice(point_list)
            x, y = self.scatter_inside(x, y, 0.05)
            self._center_diffusion_points.add((x, y))

    def heart_function(self, t, shrink_ratio: float = IMAGE_ENLARGE):
        # 返回爱心的标准坐标
        x = 16 * (sin(t) ** 3)
        y = -(13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t))
        x *= shrink_ratio
        y *= shrink_ratio
        # 坐标转换
        x += CANVAS_CENTER_X
        y += CANVAS_CENTER_Y
        return int(x), int(y)

    def scatter_inside(self, x, y, beta=0.15):
        # 抖动
        ratio_x = - beta * log(random.random() + 0.1) * (x - CANVAS_CENTER_X)
        ratio_y = - beta * log(random.random() + 0.1) * (y - CANVAS_CENTER_Y)
        dx = ratio_x * (random.random() - 0.5)
        dy = ratio_y * (random.random() - 0.5)
        return x - dx, y - dy

    def shrink(self, x, y, ratio):
        # 挤压
        force = 1 / (((x - CANVAS_CENTER_X) ** 2 + (y - CANVAS_CENTER_Y) ** 2) ** 0.520)
        dx = ratio * force * (x - CANVAS_CENTER_X) + random.randint(-1, 1)
        dy = ratio * force * (y - CANVAS_CENTER_Y) + random.randint(-1, 1)
        return x - dx, y - dy

    def calc_position(self, x, y, ratio):
        # 挤压后坐标计算
        force = 1 / (((x - CANVAS_CENTER_X) ** 2 + (y - CANVAS_CENTER_Y) ** 2) ** 0.520) * ratio
        dx = ratio * force * (x - CANVAS_CENTER_X) + random.randint(-1, 1)
        dy = ratio * force * (y - CANVAS_CENTER_Y) + random.randint(-1, 1)
        return x - dx, y - dy

    def curve(self, p):
        # 自定义曲线函数，调整跳动周期
        return 2 * (2 * sin(4 * p)) / (2 * pi)

    def calc(self, generate_frame):
        # 计算动态点位
        ratio = 10 * self.curve(generate_frame / 10 * pi)
        halo_radius = int(4 + 6 * (1 + self.curve(generate_frame / 10 * pi)))
        halo_number = int(3000 + 4000 * abs(self.curve(generate_frame / 10 * pi) ** 2))

        all_points = []

        # 光环效果
        heart_halo_point = set()  # 光环点坐标集合
        for _ in range(halo_number):
            t = random.uniform(0, 2 * pi)  # 随机时间参数
            x, y = self.heart_function(t, shrink_ratio=11.6)  # 得到爱心坐标
            x, y = self.shrink(x, y, halo_radius)  # 挤压
            if (x, y) not in heart_halo_point:
                # 处理新的点坐标加入集合
                heart_halo_point.add((x, y))
                x, y = self.scatter_inside(x, y, beta=0.15)
                size = random.randint(1, 2)
                all_points.append((x, y, size))

        # 轮廓扩散效果
        for x, y in self._points:
            x, y = self.calc_position(x, y, ratio)
            size = random.randint(1, 3)
            all_points.append((x, y, size))

        # 边缘扩散效果
        for x, y in self._edge_diffusion_points:
            x, y = self.calc_position(x, y, ratio)
            size = random.randint(1, 2)
            all_points.append((x, y, size))

        # 中心扩散效果
        for x, y in self._center_diffusion_points:
            x, y = self.calc_position(x, y, ratio)
            size = random.randint(1, 2)
            all_points.append((x, y, size))

        self.all_points[generate_frame] = all_points

    def render(self, canvas, frame):
        # 渲染一帧画面
        canvas.delete('all')
        for x, y, size in self.all_points[frame % self.generate_frame]:
            canvas.create_oval(x, y, x + size, y + size, width=0, fill=HEART_COLOR)
        # 在心的中间添加文字
        canvas.create_text(CANVAS_CENTER_X, CANVAS_CENTER_Y, text=self.text, fill=self.text_color, font=self.text_font)

def draw(main: Tk, render_canvas: Canvas, render_heart: Heart, render_frame=0):
    # 逐帧渲染
    render_canvas.delete('all')
    render_heart.render(render_canvas, render_frame)
    main.after(160, draw, main, render_canvas, render_heart, render_frame + 1)

if __name__ == '__main__':
    root = Tk()  # 初始化tk
    root.title('表白气球')  # 窗口标题
    canvas = Canvas(root, bg='black', height=CANVAS_HEIGHT, width=CANVAS_WIDTH)
    canvas.pack()
    heart = Heart()  # 爱心
    draw(root, canvas, heart)  # 开始心跳
    root.mainloop()
